﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace DelCompany
{
    public partial class CheckData : Form
    {
        private string checkType = "INFO";

        public CheckData()
        {
            InitializeComponent();
        }

        public CheckData(string type)
        {
            InitializeComponent();

            checkType = type;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void GetCompany()
        {
            string conn = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString.Replace("{uid}", GlobalAttrib.connUID).Replace("{pass}", GlobalAttrib.connPASS);

            try
            {
                string[] sqlArray = File.ReadAllText(GlobalAttrib.CompanySelFile).Replace("{companyID}", GlobalAttrib.CompanyID).Split('|');
                DataTable dt = new DataTable();
                foreach (string sql in sqlArray)
                {
                    dt = DelCompanyDal.Company.GetCompanyInfo(conn, sql);

                    outResult(sql+"\n",System.Drawing.Color.Black);
                    outResult("共查到"+dt.Rows.Count+"条记录\n",System.Drawing.Color.Green);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void GetInfo()
        {
            string conn = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString.Replace("{uid}", GlobalAttrib.connUID).Replace("{pass}", GlobalAttrib.connPASS);

            try
            {
                string[] sqlArray = File.ReadAllText(GlobalAttrib.MessageSelFile).Replace("{InfoID}", GlobalAttrib.InfoID).Split('|');
                DataTable dt = new DataTable();
                foreach (string sql in sqlArray)
                {
                    dt = DelCompanyDal.Message.GetMessageInfo(conn, sql);

                    outResult(sql + "\n", System.Drawing.Color.Black);
                    outResult("共查到" + dt.Rows.Count + "条记录\n", System.Drawing.Color.Green);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void outResult(string txt,Color color)
        {
            this.rtbCheckResult.SelectionColor = color;
            this.rtbCheckResult.AppendText(txt);
        }

        private void CheckData_Shown(object sender, EventArgs e)
        {
            if (checkType.ToUpper() == "COMPANY")
            {
                GetCompany();
            }
            else if (checkType.ToUpper() == "INFO")
            {
                GetInfo();
            }
            else
            {
                this.rtbCheckResult.Text = "数据错误.";
            }
        }
    }
}
